'use strict';

const softDelete = require('mongoosejs-soft-delete');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const Task = new Schema(
    {
      type: { type: String, required: true },
      name: { type: String, required: true },
      params: { type: Object, default: {} },
      headers: { type: Object, default: {} },
      contentField: { type: String, default: '' },
      data: { type: Object, default: {} },
      cronExpr: { type: String },
      interval: { type: Number, default: 10 },
      createdAt: { type: Date, immutable: true },
      updatedAt: { type: Date, select: false },
    },
    { timestamps: true, versionKey: false }
  );

  Task.index({ type: 1 });
  Task.plugin(softDelete);
  return app.buildValidateOfModel(mongoose.model('Task', Task));
};
